package org.tea.inventory.admin.web.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.excel.ExportExcel;
import org.tea.inventory.domain.tea.Product;
import org.tea.inventory.domain.tea.ProductCategory;
import org.tea.inventory.model.vo.SupplierInfoVo;
import org.tea.inventory.service.ISupplierInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商信息Controller
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
@Api(tags = "基础设置-供应商信息控制器")
@Slf4j
@RestController
@RequestMapping("/admin/supplier-info")
@RequiredArgsConstructor
public class SupplierInfoController extends BaseController
{
    @Autowired
    private ISupplierInfoService supplierInfoService;

    /**
     * 查询供应商信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取商品属性列表")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse page(SupplierInfoVo supplierInfoVo)
    {
        log.info("supplierInfoVo=" + supplierInfoVo);
        startPage();
        return getDataTable(supplierInfoService.selectSupplierInfoList(supplierInfoVo));
    }

    /**
     * 查询供应商信息列表(不分页)
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询供应商信息列表(不分页)")
    @ApiResponses(
            @ApiResponse(code = 0 , message = "成功" , response = ProductCategory.class)
    )
    public BaseResponse list(SupplierInfoVo supplierInfoVo) {
        List<SupplierInfoVo> list = supplierInfoService.selectSupplierInfoList(supplierInfoVo);
        return ResponseUtil.ok(list);
    }

    /**
     * 查询供应商类型列表
     */
    @GetMapping("/list/type")
    @ApiOperation(value = "查询供应商类型列表")
    public BaseResponse listType(SupplierInfoVo supplierInfoVo)
    {
        return ResponseUtil.ok(supplierInfoService.selectSupplierTypeList(supplierInfoVo));
    }

    /**
     * 导出供应商信息列表
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出供应商信息列表")
    public void exportProductCategory(SupplierInfoVo supplierInfoVo, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String title = "供应商列表";
        String header = "供应商编号,供应商名称,供应商类别,地域,联系人,申请部门,登记时间,是否启用";
        String column = "supplierNum,supplierName,typeName,supplierPurchasAddress,supplierContact,deptName,addTimeStr,isEnableStr";
        map.put("biaoti", title);
        map.put("header", header);
        map.put("column", column);
        try {
            List<SupplierInfoVo> productCategories = supplierInfoService.selectSupplierInfoList(supplierInfoVo);
            List<Map> list = JSONArray.parseArray(JSON.toJSONString(productCategories), Map.class);
            ExportExcel.export(request, response, list, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 新增供应商信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增供应商信息")
    public BaseResponse add(@RequestBody SupplierInfoVo supplierInfoVo)
    {
        log.info("add supplierInfoVo=" + supplierInfoVo);
        return ResponseUtil.ok(supplierInfoService.insertSupplierInfo(supplierInfoVo));
    }

    /**
     * 修改供应商信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改供应商信息")
    public BaseResponse edit(@RequestBody SupplierInfoVo supplierInfoVo)
    {
        return ResponseUtil.ok(supplierInfoService.updateSupplierInfo(supplierInfoVo));
    }

    /**
     * 删除供应商信息
     */
    @PostMapping(value = "/remove")
    @ApiOperation(value = "删除供应商信息")
    public BaseResponse remove(Long id)
    {
        return ResponseUtil.ok(supplierInfoService.deleteSupplierInfoById(id));
    }

    /**
     * 更新物流公司状态
     */
    @PostMapping(value = "/updateStatus")
    @ApiOperation(value = "更新供应商状态")
    public BaseResponse updateStatus(@ApiParam(value = "更新id") Long id , @ApiParam(value = "更新前状态") Integer isEnable)
    {
        return ResponseUtil.ok(supplierInfoService.updateStatus(id , isEnable));
    }
}
